<template>
	<view class="content-prize">
		<stopModal />
		<cu-custom :noDiv="false">
		    <block slot="content" style="color: #FFFFFF;font-size: 20px;">兑奖品</block>
		</cu-custom>
		<view class="top-container">
			<view class='top-bkg'>
				<img class="bkg-img" src="@/static/img/img_homepage_top_banner_01.jpg" alt="">
			</view>
		</view>
		
		<view class="shop-list">
			<view class="title-area">
				<img class="title-img" src="@/static/img/img_title_exchange@2x.png" alt="">
			</view>
			<view class="items">
				<!-- 任务的循环展示 -->
				<view v-for="(item, index) in dataList" :key="index">
					<view class="item-top"/>
					<view class="item"  :class="{unfinish : item.type== 1 , unclaimed: item.type ==  2 , finish: item.type ==  3 } ">
						
						<img v-if="item.cover_url" class="item-img" :src="item.cover_url" alt="">
						<img v-else class="item-img" src="../../static/img/img_goods_empty.jpg" alt="">
						<view class="item-desc">
							<view class="desc-title">{{item.title}}</view>
							<view class="desc-price">
								<view>
									<span class="now">¥{{item.nowPrize || 0}}</span>
									<span class="old">原价{{item.price}}</span>
								</view>
								<view class="num">剩余：<span class="num-num">{{setNum(item)}}</span></view>
							</view>
							<view class="desc">{{item.sub_title}}</view>
							<view class="btn" @click="showLottery(item)">
								<!-- {{item.lottery_point}}积分抽奖 -->
								兑换&抽奖
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="no-data">
				<view class="no-data-desc">-更多兑换正在准备中-</view>
				<img class="no-data-img" src="@/static/img/img_48_page_btm_tips@2x.png" alt="没有数据图片">
			</view>
		</view>
		<view v-if="lotteryModeFlag">
			<view v-if="modeType == 'lottery_modal'">
				<checkLottery :checkItem="checkItem"/>
			</view>
			<view v-else-if="modeType == 'lottery_result_modal'">
				<lotteryResultModal :lotteryResult="lotteryResult"/>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import stopModal from "@/components/modal/stop_modal.vue";
	import countDowm from '@/components/countdown.vue'
	import checkLottery from '@/components/lottery_modal/check_lottery.vue'
	import lotteryResultModal from '@/components/lottery_modal/lottery_result_modal.vue'
	
	export default {
		components: {
			countDowm,
			checkLottery,
			lotteryResultModal,
			stopModal
		},
		computed:{
			...mapState(['authflag'])
		},
		
		data() {
			return {
				authType:['getUserInfo', 'getPhoneNumber'],
				title: 'Hello',
				lotteryModeFlag: false,
				modeType: null,
				// modeType: 'lottery_modal',
				checkItemCount: 0,
				lotteryResult: {
					type: 6 // 1 表示抽奖成功， 2 表示重复抽奖 3 表示 商品抽完 4 表示 活动结束 5 表示积分不足 6 表示兑换成功
				},
				checkItemVideoStatus: 2,
				checkItem: {
					// cover_url: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2911844190,4020192584&fm=173&app=49&f=JPEG?w=640&h=355&s=62E69C520B587DCA5B44C54B030070F3",
					// created_at: 0,
					// description: "11",
					// end_time: 1711039104,
					// exchange_point: 1000,
					// exchange_quantity: 90,
					// exchange_quantity_total: 1, // 已经兑换的
					// id: 1,
					// lottery_point: 500,
					// lottery_quantity: 10,
					// lottery_quantity_total: 1, // 抽奖兑换的
					// price: 1000,
					// sku_id: "11111",
					// sort: 0,
					// start_time: 1611039104,
					// status: 1,
					// sub_title: "subtitle",
					// title: "奖品title",
					// total_quantity: 100, //总数
					// updated_at: 0,
					// user_count: 0,
				},
				dataList: []
			}
		},
		async onLoad(option) {
			//等待登录成功	
			await this.$onLaunched;
			console.log('页面逻辑开始了')
			// 后续业务逻辑
			this.getList()
			uni.$on("pop_close", () => {
				this.lotteryModeFlag = false
				this.getList()
			});
			uni.$on("set_lottery_result", (data) => {
				this.lotteryResult = data
				this.modeType = 'lottery_result_modal'
			});
			uni.$on("refresh_prize_list", () => {
				this.getList()
			});
			
		},
		onHide(){
			this.modeType = null
			this.lotteryModeFlag = false
		}, 
		onPullDownRefresh() {
		    console.log('refresh');
		    this.getList()
			setTimeout(function () {
			    uni.stopPullDownRefresh();
			}, 1000);
		},
		onShow() {
			this.getList()
		},
		methods: {
			setNum(item){
				let num = 0
				num = item.total_quantity - item.exchange_quantity_total -item.lottery_quantity_total
				return num || 0
			},
			getList(type){
				if(!type){
					this.$api.getPrizeList().then(res => {
						this.dataList =res.items
					})
				}
				
				setTimeout(() => {
					uni.hideLoading()
				}, 3000)
			},
			asd(){
				return `<view>测试内筒</view>`	
			},
			showLottery(item){
				this.lotteryModeFlag = true
				this.checkItem = item
				this.modeType = 'lottery_modal'
				console.log('开始积分兑换')
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #FFFFFF;
	}
	.content-prize{
		background-color: #FFFFFF;
		min-width: 100vw;
		min-height: 66vh;
		.top-container{
			.top-bkg{
				margin-top: -120rpx;
				height: 140px;
				// background-color: #D0021B;
				.bkg-img{
					width: 100%;
					height: 100vw;
				}
			}
		}
		.shop-list{
			position: relative;
			background: #FFFFFF;
			border-radius: 20px 20px 0 0 ;
			margin-top: -20px;
			min-height: 66vh;
			margin-bottom: 80px;
			.title-area{
				.title-img{
					width: 100%;
					height: 19vw;
					margin-top: -7vw;
				}
			}
			.items{
				margin: 4px 18px;
				text-align: center;
				// min-height: 100vh;
				// padding-bottom: 100px;
				.item-top{
					width: 100%;
					height: 12px;
				}
				.item{
					display: flex;
					border-radius: 10px;
					.item-img{
						width: 34vw;
						height: 34vw;
					}
					.item-desc{
						border-bottom: 1px solid #E1E1E1;
						flex: 1;
						text-align: left;
						margin-left: 12px;
						.num-area{
							height: 24px;
							display: flex;
							justify-content: space-between;
							align-items: center;
							.num{
								font-size: 12px;
								color: #999999;
								span{
									font-size: 12px;
									color: #F82045;
								}
							}
						}
						.desc-title,.desc-price{
							margin-top: 4px;
							height: 20px;
							line-height: 20px;
							font-size: 14px;
							color: #333333;
							max-width: calc(100vw -  180px);
							overflow: hidden;
							text-overflow:ellipsis;
							white-space: nowrap;
						}
						.desc-price{
							margin-top: 22rpx;
							display: flex;
							justify-content: space-between;
							font-size: 12px;
							color: #999999;
							font-weight: 500;
							.now{
								font-size: 14px;
								color: #F82045;
								line-height: 20px;
								margin-right: 4px;
							}
							.old{
								font-size: 12px;
								color: #999999;
								line-height: 20px;
								text-decoration: line-through;
							}
							.num-num{
								color: #F82045;
							}
						}
						.desc{
							font-size: 12px;
							color: #999999;
							line-height: 16px;
							margin-top: 4px;
							max-width: calc(100vw - 180px);
							overflow: hidden;
							text-overflow:ellipsis;
							white-space: nowrap;
							
							// display: -webkit-box;
							// -webkit-box-orient: vertical;
							// -webkit-line-clamp: 2;
							// overflow: hidden;
						}
						.btn{
							// width: 40.5vw;
							width: 36.4vw;
							height: 13.8vw;
							letter-spacing: 3rpx;
							line-height: 13.8vw;
							background-image: url('@/static/btn/btn_36_red_144_shadow_nor@2x.png');
							background-size: 36.4vw 13.8vw;
							text-align: center;
							margin-left: -3vw;
							font-size: 4vw;
							color: #FFFFFF;
							text-shadow: 0 1px 2px rgba(0,0,0,0.25);
							margin-top: 22rpx;
							font-weight: 500;
						}
					}
				}
			}
			.no-data{
				background: #FFFFFF;
					position: fixed;
					z-index: -1;
					line-height: 0;
					bottom: 0;
					width:100vw;
					text-align: center;
					.no-data-desc{
						font-size: 12px;
						color: #959595;
						text-align: center;
						position: relative;
						top: 64px;
					}
					.no-data-img{
						width: 240px;
						height: 48px;
						margin-top: 43px;
					}
				}
			
		}
	}
</style>
